package br.comdecorando.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import br.com.decorando.model.Perfil;

import br.com.decorando.activity.R;

public class PerfilAdapter extends ArrayAdapter<Perfil> {
	private List<Perfil> itens;
	private int layoutResourceId;
	private Context context;	
	private PerfilHolder editHolder;
	
	public PerfilAdapter(Context context, int layoutResourceId, List<Perfil> itens) {
		super(context, layoutResourceId, itens);
		this.context = context;
		this.layoutResourceId = layoutResourceId;
		this.itens = itens;
	}
	
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		PerfilHolder holder = new PerfilHolder();
		holder.perfil = itens.get(position);		
		LayoutInflater inflater = ((Activity) context).getLayoutInflater();
		View row = inflater.inflate(layoutResourceId, parent, false);
		setComponents(holder, row);
		Log.d("View", String.valueOf(holder.getPerfil().isAtivo()));
		if(holder.equals(editHolder)){
			configEdit(holder);	
			
		}else if(!holder.perfil.isAtivo()){
			configActive(holder);
		}
		fillComponents(holder);			
		row.setTag(holder);
		return row;
	}		
	
	public void setComponents(PerfilHolder holder, View row){
		holder.nome = (TextView) row.findViewById(R.id.txvNomePerfil);		
		holder.editar = (ImageButton) row.findViewById(R.id.btnEditar);
		holder.desativar = (ImageButton) row.findViewById(R.id.btnDesativar);
		holder.ativar = (ImageButton) row.findViewById(R.id.btnAtivar);
		holder.cancelar = (ImageButton) row.findViewById(R.id.btnCancelar);
	}
	
	public void fillComponents(PerfilHolder holder){
		holder.nome.setText(holder.perfil.getName());
		holder.editar.setTag(holder);
		holder.ativar.setTag(holder);
		holder.desativar.setTag(holder);
		holder.cancelar.setTag(holder);
	}
	
	public void configEdit(PerfilHolder holder){
		holder.nome.setTextColor(Color.GREEN);
		holder.editar.setVisibility(View.GONE);
		holder.desativar.setVisibility(View.INVISIBLE);		
		holder.cancelar.setVisibility(View.VISIBLE);
	}
	
	public void configActive(PerfilHolder holder){
		holder.nome.setTextColor(Color.RED);
		holder.editar.setVisibility(View.GONE);
		holder.desativar.setVisibility(View.INVISIBLE);		
		holder.ativar.setVisibility(View.VISIBLE);
	}
	
	@Override
	public void add(Perfil object) {		
		super.add(object);
		super.notifyDataSetChanged();
	}
	
	public Perfil getEditPerfil() {
		return editHolder.perfil;
	}

	public void setEditHolder(PerfilHolder holder) {
		this.editHolder = holder;
		notifyDataSetChanged();
	}
	
	public boolean isEdit(){
		return editHolder != null;
	}
	
	public static class PerfilHolder{
		private Perfil perfil;
		private TextView nome;
		private ImageButton editar;
		private ImageButton desativar;
		private ImageButton ativar;
		private ImageButton cancelar;
		
		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result
					+ ((perfil == null) ? 0 : perfil.hashCode());
			return result;
		}
		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			PerfilHolder other = (PerfilHolder) obj;
			if (perfil == null) {
				if (other.perfil != null)
					return false;
			} else if (!perfil.equals(other.perfil))
				return false;
			return true;
		}	
		
		public String getNome(){
			return perfil.getName();
		}
		
		public Perfil getPerfil(){
			return perfil;
		}
	}
}
